package ull;

import model.Attraction;
import model.Road;
import udl.OpreationAttraction;
import udl.OpreationRoad;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class AttractionLogic {
    OpreationAttraction opreationAttraction=new OpreationAttraction();
    OpreationRoad opreationRoad=new OpreationRoad();
    //检查景点名是否相同
    public boolean checkText(String text1, String text2,Integer text3){
        boolean judge =false;
        if(opreationAttraction.ifExist(text1)&&opreationAttraction.ifExist(text2)){
            opreationRoad.addRoad(text1,text2,text3);
            judge=true;
        }
        return judge;
    }
    //检查点的边界
    public boolean checkBounds(int x, int y) {
        return x <= 700 && x >= 50 && y <= 700 && y >= 50;
    }
    //获取点的名称
    public String[] getPointAnnotations(){
        int num = opreationAttraction.returnnum();
        int i = 0;
        List<Attraction> list;
        list = opreationAttraction.selectViews();
        String[] pointAnnotations=new String[num];
        for (Attraction attraction : list) {
            pointAnnotations[i]= String.valueOf(attraction.getName());
            i++;
        }
        return pointAnnotations;
    }
    //获取点的坐标
    public int[][] getPointCoordinates(){
        int num = opreationAttraction.returnnum();
        int i = 0;
        List<Attraction> list;
        list = opreationAttraction.selectViews();
        int[][] pointCoordinates=new int[num][2];
        for (Attraction attraction : list) {
            pointCoordinates[i][0]=attraction.getX();
            pointCoordinates[i][1]=attraction.getY();
            i++;
        }
        return pointCoordinates;
    }
    //检查该地点是否存在景点
    public boolean checkAttraction(Attraction att) {
        return !opreationAttraction.check(att);
    }
    //是否存在该景点
    public boolean ifExist(String name) {
        return opreationAttraction.ifExist(name);
    }
    //添加景点
    public void addAttraction(Attraction att) throws SQLException {
        opreationAttraction.addAttraction(att);
    }
    //获取所有景点的名称
    public String[] getAttractionName() {
        int num = opreationAttraction.returnnum();
        int i = 0;
        String[] str = new String[num];
        List<Attraction> list;
        list = opreationAttraction.selectViews();
        for (Attraction attraction : list) {
            str[i] = attraction.getName();
            i++;
        }
        return str;
    }
    //是否景点删除成功
    public boolean deleteView(String string) {
        return opreationRoad.deleteView(string);
    }
    //获取所有景点的名称
    public String getAttractionNames() {
        String[] strings=getAttractionName();
        StringBuilder str= new StringBuilder("\n\n");
        for (String string : strings) {
            str.append("\t\t").append(string).append("\n");
        }
        return str.toString();
    }
    //修改景点
    public void modifyAttraction(Attraction att) {
        opreationAttraction.modifyAttraction(att);
    }
    //修改起始点坐标
    public void modifyRoad1(Attraction att) {
        opreationAttraction.modifyRoad1(att);
    }
    //修改终点坐标
    public void modifyRoad2(Attraction att) {
        opreationAttraction.modifyRoad2(att);
    }
    //检查是否存在该路线
    public boolean checkText1(String text1, String text2) {
        return opreationRoad.ifExistRoad(text1, text2);
    }
    //检查距离
    public boolean checkDistance(int distance) {
        return distance > 1500 || distance < 0;
    }
    //获取显得注释
    public String[] getLineAnnotations(){
        List<Road> list;
        list = opreationRoad.selectViews1();
        int num = opreationRoad.returnnum1();
        String[] lineAnnotations=new String[num];
        int j = 0;
        for (Road road : list) {
            lineAnnotations[j]= String.valueOf(road.getDistance());
            j++;
        }
        return lineAnnotations;
    }
    //获取线的坐标
    public int[][] getLineCoordinates(){
        List<Road> list = opreationRoad.selectViews1();
        int num = opreationRoad.returnnum1();
        int[][] lineCoordinates=new int[num][4];
        int j = 0;
        for (Road road : list) {
            lineCoordinates[j][0]=road.getStartX();
            lineCoordinates[j][1]=road.getStartY();
            lineCoordinates[j][2]=road.getEndX();
            lineCoordinates[j][3]=road.getEndY();
            j++;
        }
        return lineCoordinates;
    }
    //删除路线
    public void deleteRoad1(String string) {
        opreationRoad.deleteRoad1(string);
    }
    //修改路线
    public void modifyRoad(String text1, String text2, Integer text3) {
        opreationRoad.modifyRoad(text1,text2,text3);
    }
    //获取路径的起始点
    public String[] getRoadStart() {
        List<Road> list1;
        list1 = opreationRoad.selectViews1();
        int num1=opreationRoad.returnnum1();
        String[] str=new String[num1];
        int j=0;
        for (Road road : list1) {
            str[j] = road.getStartRoad();
            j++;
        }
        return str;
    }
    //获取路径的终点
    public String[] getRoadEnd() {
        List<Road> list1;
        list1 = opreationRoad.selectViews1();
        int num1=opreationRoad.returnnum1();
        String[] str=new String[num1];
        int j=0;
        for (Road road : list1) {
            str[j] = road.getEndRoad();
            j++;
        }
        return str;
    }
    //获取路径的距离
    public int[] getRoadDistance() {
        List<Road> list;
        list = opreationRoad.selectViews1();
        int num=opreationRoad.returnnum1();
        int[] distance=new int[num];
        int i=0;
        for (Road road : list) {
            distance[i] = road.getDistance();
            i++;
        }
        return distance;
    }
    //删除路径
    public void deleteRoad(String text1, String text2) {
        opreationRoad.deleteRoad(text1,text2);
    }

    public boolean ifExistRoad(String text1, String text2) {
        return opreationRoad.ifExistRoad(text1,text2);
    }
    //获取最短路径
    public String getShortestDistance(String text1, String text2) {
        final int inf=10000000;
        String[] str = getAttractionName();
        int num=opreationAttraction.returnnum();
        int[] book = new int[num];
        int[][] map = new int[num][num];
        int num1 = opreationRoad.returnnum1();
        String[] str1 =getRoadStart();
        String[] str2 =getRoadEnd();
        int[] dis = getRoadDistance();
        //初始化
        for (int t1 = 0; t1 < num; t1++) {
            for (int t2 = 0; t2 < num; t2++) {
                if (t1 == t2) {
                    map[t1][t2] = 0;
                } else {
                    map[t1][t2] = inf;
                }
            }
        }
        for (int k = 0; k < num1; k++) {
            int t1 = 0, t2 = 0;
            for (int k1 = 0; k1 < num; k1++) {
                if (str1[k].equals(str[k1])) {
                    t1 = k1;
                }
                if (str2[k].equals(str[k1])) {
                    t2 = k1;
                }
            }
            if (map[t1][t2] > dis[k]) {
                map[t1][t2] = dis[k];
                map[t2][t1] = dis[k];
            }
        }
        int start = 0, end = 0;
        for (int k = 0; k < num; k++) {
            if (text1.equals(str[k])) {
                start = k;
            }
            if (text2.equals(str[k])) {
                end = k;
            }
        }
        dis = new int[num];
        int[] path = new int[num];
        System.arraycopy(map[start], 0, dis, 0, num);
        book[start] = 1;
        Arrays.fill(path, -1);
        for (int k = 0; k < num; k++) {
            int min = inf, temp = 0;
            for (int k1 = 0; k1 < num; k1++) {
                if (book[k1] == 0) {
                    if (dis[k1] < min) {
                        min = dis[k1];
                        temp = k1;
                    }
                }
            }
            book[temp] = 1;
            if (temp == end) {
                break;
            }
            for (int k1 = 0; k1 < num; k1++) {
                if (book[k1] == 0 && map[temp][k1] < inf) {
                    if (dis[k1] > dis[temp] + map[temp][k1]) {
                        dis[k1] = dis[temp] + map[temp][k1];
                        path[k1] = temp;
                    }
                }
            }
        }
        StringBuilder string = new StringBuilder(" ");
        string.insert(0, str[end]);
        int it = end;
        while (path[end] != -1) {
            for (int k = 0; k < num; k++) {
                if (str[path[end]].equals(str[k])) {
                    string.insert(0, str[path[end]] + "->");
                }
            }
            end = path[end];
        }
        string.insert(0, str[start] + "->");
        int di;
        if (dis[it] == inf) {
            string.append("\n永远无法到达\t");
        } else {
            di = dis[it];
            string.append("\n最短路径距离为：").append(di).append("\t");
        }
        return string.toString();
    }
}
